/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import pojo.FtpInfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.LogFile;

/**
 *
 * @author ahmed
 */
//this class for all FTP operations that should be made in database...
public class FtpInfoCrud {

    Connection conn;
    PreparedStatement stmt;

    public FtpInfoCrud(Connection connection) {
        conn = connection;
    }
    //this method for updating existing ftp data

    public boolean update(FtpInfo newFtpInfo, int id) {
        boolean check = false;
        ProviderDao pro = new ProviderDao(conn);
        try {
            boolean first = false;
            String updatetxt = "";
            if (newFtpInfo.getHost() != null) {
                first = true;
                updatetxt = "host='" + newFtpInfo.getHost() + "'";
            }
            if (newFtpInfo.getRemoteDir() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "remote_dir='" + newFtpInfo.getRemoteDir() + "'";
            }
            if (newFtpInfo.getFileName() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "file_name='" + newFtpInfo.getFileName() + "'";
            }
            if (newFtpInfo.getFileExtension() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "file_extension='" + newFtpInfo.getFileExtension() + "'";
            }
            if (newFtpInfo.getUserName() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "user_name='" + newFtpInfo.getUserName() + "'";
            }
            if (newFtpInfo.getPassword() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "password='" + newFtpInfo.getPassword() + "'";
            }
            if (newFtpInfo.getDateMod() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "date_mod='" + newFtpInfo.getDateMod() + "'";
            }
            if (newFtpInfo.getLocalDir() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "local_dir='" + newFtpInfo.getLocalDir() + "'";
            }
            stmt = conn.prepareStatement("update ftp_info set " + updatetxt + "where ftp_id=? ;");
            stmt.setInt(1, id);
            int i = stmt.executeUpdate();
            if (i == 1) {
                check = true;
            }
        } catch (SQLException e) {
            check = false;
            LogFile.log(" System ", " found the following error while trying update FTP Info of provider " + pro.getById(id).getName() + " the error is " + e.getMessage());
            e.printStackTrace();
        }
        return check;
    }

    //this method for adding new FTP information in database...
    public boolean add(FtpInfo newFtpInfo) {
        boolean check = false;
        try {
            System.out.println(newFtpInfo.getHost());
            stmt = conn.prepareStatement("insert into ftp_info values(?,?,?,?,?,?,?,?,?)");
            stmt.setInt(1, newFtpInfo.getFtpId());
            stmt.setString(2, newFtpInfo.getHost());
            stmt.setString(3, newFtpInfo.getRemoteDir());
            stmt.setString(4, newFtpInfo.getFileName());
            stmt.setString(5, newFtpInfo.getFileExtension());
            stmt.setString(6, newFtpInfo.getUserName());
            stmt.setString(7, newFtpInfo.getPassword());
            stmt.setString(8, newFtpInfo.getDateMod());
            stmt.setString(9, newFtpInfo.getLocalDir());
            int i = stmt.executeUpdate();
            if (i == 1) {
                check = true;
                LogFile.log(" System ", " successfully add data for FTP server with IP : ["+newFtpInfo.getHost()+"]");
            } else {
                check = false;
                LogFile.log(" System ", " failed to add data for FTP Server with IP : ["+newFtpInfo.getHost()+"]");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            LogFile.log(" System ", " encountered the following exception trying to add FTP Server with IP:["+newFtpInfo.getHost()+"]to its table in database ,the Problem is : [" + e+"]");
        }
        return check;
    }

    public FtpInfo selectById(int id) {
        FtpInfo ftpInfo = null;
        try {
            stmt = conn.prepareStatement("SELECT * FROM ftp_info WHERE ftp_id=?");

            stmt.setInt(1, id);
            ResultSet resultSet = stmt.executeQuery();
            if (resultSet.next()) {
                ftpInfo = new FtpInfo(id);
                ftpInfo.setHost(resultSet.getString(2));
                ftpInfo.setRemoteDir(resultSet.getString(3));
                ftpInfo.setFileName(resultSet.getString(4));
                ftpInfo.setFileExtension(resultSet.getString(5));
                ftpInfo.setUserName(resultSet.getString(6));
                ftpInfo.setPassword(resultSet.getString(7));
                ftpInfo.setDateMod(resultSet.getString(8));
                ftpInfo.setLocalDir(resultSet.getString(9));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ftpInfo;
    }

    public boolean deleteById(int id) {

        try {
            stmt = conn.prepareStatement("delete from ftp_info where ftp_id=?");
            stmt.setInt(1, id);
            return stmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;

    }

    public static void main(String[] args) {
    }

    //method to retreive ftp data based on provider name
    public FtpInfo getByProviderName(String providerName) {
        FtpInfo ftpInfo = null;
        try {
            stmt = conn.prepareStatement("select * from ftp_info where ftp_id =(select p_id from providers where name =?)");
            stmt.setString(1, providerName.trim());
            ResultSet resultSet = stmt.executeQuery();
            if (resultSet.next()) {
                ftpInfo = new FtpInfo();
                ftpInfo.setFtpId(resultSet.getInt(1));
                ftpInfo.setHost(resultSet.getString(2));
                ftpInfo.setRemoteDir(resultSet.getString(3));
                ftpInfo.setFileName(resultSet.getString(4));
                ftpInfo.setFileExtension(resultSet.getString(5));
                ftpInfo.setUserName(resultSet.getString(6));
                ftpInfo.setPassword(resultSet.getString(7));
                ftpInfo.setDateMod(resultSet.getString(8));
                ftpInfo.setLocalDir(resultSet.getString(9));
                System.out.println("Ftp File Exten : " + ftpInfo.getFileExtension());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ftpInfo;
    }
}
